<template>
	<view class="box">
		<u-popup :show="updatePopupShow" mode="center" @close="updatePopupClose" @open="updatePopupOpen"
			bgColor="transparent" overlayStyle="background:rgba(0,0,0,0.5)">
			<view class="popup-box">
				 
				<view class="popup-img">
					<image src="../../static/img/update-top.png"></image>
				</view>
				
				<view class="popup-main">
					<view class="popup-title">
						发现新版本
					</view>
					<view class="popup-subhead">
						版本号：{{version}}
					</view>
					
					<view class="popup-center">
					
						<view class="popup-center-title">
							更新内容:
						</view>
					
						<view class="popup-center-text" v-for="(item,index) in updateMsg" :key="index">
							{{ index + 1 + '、' + item}}
						</view>
					
					</view>
					
					<view class="popup-now-update" v-if="!ifNowUpdate">
						<u-button type="success" text="立即更新" class="popup-now-update-button" @click="nowUpdate"></u-button>
					</view>
					
					<view class="popup-download" v-if="ifNowUpdate">
						<view class="popup-download-title">正在下载</view>
						<u-line-progress :percentage="updateProgress" activeColor="rgba(74, 188, 90, 1)"></u-line-progress>
					</view>
				</view>

			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		updateApi
	} from '../../api/index.js'
	import {appUpdate} from '../../utility/download.js'
	export default {
		data() {
			return {
				updateProgress: 0, //更新进度
				updateMsg: '', //更新信息
				updatePopupShow: true,
				ifNowUpdate:false,
				version:'',
			}
		},
		onLoad() {
			this.getUpdateMessage()
		},
		methods: {

			//弹窗关闭
			updatePopupClose() {

			},

			//弹窗打开
			updatePopupOpen() {

			},
			
			//立即更新点击
			nowUpdate(){
				this.getUpdate()
				this.ifNowUpdate = !this.ifNowUpdate
			},
			
			//获取更新内容
			async getUpdateMessage(){
				try{
					const resUpdate = await appUpdate()
					this.updateMsg = resUpdate.data.intro
					this.version = resUpdate.data.version
				}catch(e){
					//TODO handle the exception
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
				}
			},
			
			// 获取更新信息
			async getUpdate() {
				try{
					const resUpdate = await appUpdate()
					// 判断是否为热更新
					if (resUpdate.data.updateType === 0) {
						this.downloadFun(resUpdate.data.address, 'wgt')
					} else {
						//整包更新
						//判断系统
						if (plus.os.name === 'Android') {
							this.downloadFun(resUpdate.data.address, 'apk')
						} else {
							//ios 调起应用商店
							console.log(resUpdate)
							plus.runtime.launchApplication({
								action: resUpdate.data.address
							}, function(fail) {
								console.log('唤醒失败：', fail)
								uni.showToast({
									title: '唤起失败，请手动打开苹果应用商店',
									icon: 'none'
								})
							});
						}
					}
				}catch(e){
					//TODO handle the exception
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
				}
			},

			//下载安装
			downloadFun(url, type) {
				const downloadTask = uni.downloadFile({
					url: url,
					success: (downloadRes) => {
						console.log(downloadRes)
						// 判断状态码是否200系列
						if (downloadRes.statusCode < 200 || downloadRes.statusCode > 300) {
							uni.showToast({
								icon: 'none',
								title: '请求失败 code ' + downloadRes.statusCode
							})
							return
						}

						//热更新
						if (type === 'wgt') {
							//此处resWgt.wgtUrl 需后续改为正常地址
							plus.runtime.install(downloadRes.tempFilePath, {
									force: true
								},
								(installRes) => {
									uni.showLoading({
										mask: true,
										title: '安装中，请稍候...'
									})
									setTimeout(() => {
										uni.hideLoading()
										//应用重启
										plus.runtime.restart();
									}, 1000);
								},
								(installErr) => {
									uni.showToast({
										title: 'wgt安装失败~',
										icon: 'none'
									})
								})
						} else {
							//安卓整包更新
							//convertLocalFileSystemURL() ---将本地URL路径转换成平台绝对路径
							plus.runtime.install(downloadRes.tempFilePath, {}, {},
							// plus.runtime.install(plus.io.convertLocalFileSystemURL(downloadRes.tempFilePath), {}, {},
								(installErr) => {
									uni.showToast({
										title: '安装失败',
										duration: 1500
									});
								})

						}


					}
				});

				//赋值进度
				downloadTask.onProgressUpdate((res) => {
					this.updateProgress = res.progress
				});

			},

		}
	}
</script>

<style lang="scss" scoped>
	.box {

		.popup-box {
			width: 590rpx;
			height: 100%;
			border-radius: 20rpx;

			.popup-img {
				width: 100%;
				height: 444rpx;
				position: relative;
				image{
					width: 100%;
					height: 444rpx;
					position: absolute;
					top: 4rpx;
				}
			}
			
			.popup-main{
				
				background-color: #FFFFFF;
				padding: 0 30rpx;
				border-bottom-left-radius: 20rpx;
				border-bottom-right-radius: 20rpx;
				.popup-title{
					color: $uni-color-black;
					font-size: 38rpx;
					font-weight: bold;
					text-align: center;
					padding: 60rpx 0 20rpx 0;
				}
				
				.popup-subhead{
					color: rgba(58, 58, 58, 1);
					font-size: $uni-font-size-sm;
					text-align: center;
					margin-bottom: 30rpx;
				}
				
				.popup-center{
					.popup-center-title{
						color: $uni-color-black;
						font-weight: bold;
						font-size: $uni-font-size-base;
						margin-bottom: 30rpx;
					}
					
					.popup-center-text{
						font-size: $uni-font-size-base;
						margin-bottom: 30rpx;
						color: rgba(58, 58, 58, 1);
					}
					
				}
				
				.popup-now-update{
					padding-bottom: 40rpx;
					.popup-now-update-button{
						border-radius: 40rpx;
						font-weight: bold;
						background-color: rgba(74, 188, 90, 1);
					}
				}
				
				.popup-download{
					padding-bottom: 70rpx;
					.popup-download-title{
						color: $uni-color-black;
						font-size: $uni-font-size-sm;
						margin-bottom: 20rpx;
					}
				}
				
			}

		}

	}
</style>
